// other.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    headimg: "",
    nickname: "",
    tel: "",
    phones: true,
    appColor: app.appColor,
    // 新优惠券方案2
    disRed: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 获取用户信息
    wx.getUserInfo({
      withCredentials: true,
      success: function (res) {
        var userInfo = res.userInfo
        var nickName = userInfo.nickName
        var avatarUrl = userInfo.avatarUrl
        var gender = userInfo.gender //性别 0：未知、1：男、2：女
        var province = userInfo.province
        var city = userInfo.city
        var country = userInfo.country
        that.setData({
          headimg: avatarUrl,
          nickname: nickName,
          // tel: "18848874291"
        });
      },
      fail: function (xhr) {
        console.log(xhr);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  codeBind: function () {
    wx.navigateTo({
      url: '../bindPhone/bindPhone',
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    try{
      this.discountsData();
      this.setData({
        IsRegister: app.IsRegister,
        HandsetNumber: app.HandsetNumber
      });
      // 优惠红点显示
      wx.getStorage({
        key: 'lastDiscount',
        success:function(data){
          that.newDate = data.data;
        },
        fail:function(errs){
        }
      })
    }catch(err){}
  },
  // 2020优惠活动
  discountsData: function () {
    let that = this;
    wx.request({
      url: app.serviceurl + 'Order/GetUserCoupon',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      dataType: 'json',
      success: function (res) {
        // IsCanUse：是否可用，使用次数限制
        if (!res.data.Result) {
          that.setData({
            discount: `0张`
          });
          return 
        };
        if(res.data.UserCoupons != '' && res.data.UserCoupons != null && typeof(res.data.UserCoupons) != 'undefined'){
          try{
            that.setData({
              discount: `${res.data.UserCoupons.length}张`,
              disRed: true
            });
          }catch(err){}
        }else{
          that.setData({
            discount: `0张`,
            disRed: false
          });
        }
      },
      fail: function () {
        console.log("调用失败");
      },
      complete:function(data){
        wx.setStorage({
          key: 'lastDiscount',
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    try{
      if (res.from === 'menu') {
        return app.OnShare();
      }
    }catch(err){
      console.log(err)
    }
  },
// 跳转到新版小程序
newEdition:function(){
  wx.navigateToMiniProgram({
    appId: 'wx5c41c321ceb5c552',
    path: 'pages/carpool/book/book',
    envVersion: 'release',
  })
},
  qrcode:function(){
    wx.navigateTo({
      url: '/pages/yiqingCode/yiqingCode',
    })
  },
  discountsList:function(){
    // this.setData({
    //   newDis: false
    // });
    wx.navigateTo({
      url: '/pages/discountsList/discountsList',
    })
  },
  problem: function (e) {
    wx.navigateTo({
      url: '/pages/problem/problem',
    })
  },
  query: function (e) {
    wx.navigateTo({
      url: '/pages/query/query',
    })
  },
  callservice: function (e) {
    wx.makePhoneCall({
      phoneNumber: "96606"
    });
  },
  phoneservice: function (e) {
    wx.makePhoneCall({
      phoneNumber: "96606"
    });
  },
  contact: function (e) {
    wx.navigateTo({
      url: '/pages/contact/contact',
    })
  },
})